from gen import generate_fixed_cases, generate_test_case
from alg import Solution
import time

def test_subsets():
    """
    测试子集算法
    """
    solution = Solution()
    test_cases = generate_fixed_cases()
    
    print("=" * 50)
    print("子集算法测试结果")
    print("=" * 50)
    
    for i, nums in enumerate(test_cases, 1):
        print(f"\n测试用例 {i}:")
        print(f"输入: {nums}")
        
        # 测试回溯算法
        start_time = time.time()
        result_backtrack = solution.subsets(nums)
        time_backtrack = time.time() - start_time
        
        # 测试迭代算法
        start_time = time.time()
        result_iterative = solution.subsets_iterative(nums)
        time_iterative = time.time() - start_time
        
        print(f"回溯算法结果: {result_backtrack}")
        print(f"迭代算法结果: {result_iterative}")
        print(f"子集数量: {len(result_backtrack)}")
        print(f"回溯算法耗时: {time_backtrack:.6f}秒")
        print(f"迭代算法耗时: {time_iterative:.6f}秒")
        
        # 验证结果正确性
        expected_count = 2 ** len(nums)
        if len(result_backtrack) == expected_count:
            print("✓ 测试通过")
        else:
            print("✗ 测试失败")
    
    print("\n" + "=" * 50)
    print("性能对比测试")
    print("=" * 50)
    
    # 生成随机测试用例进行性能测试
    print(f"\n随机测试用例:")
    random_nums = generate_test_case(8)  # 生成长度为8的随机数组
    print(f"输入: {random_nums}")
    
    start_time = time.time()
    result_backtrack = solution.subsets(random_nums)
    time_backtrack = time.time() - start_time
    
    start_time = time.time()
    result_iterative = solution.subsets_iterative(random_nums)
    time_iterative = time.time() - start_time
    
    print(f"子集数量: {len(result_backtrack)}")
    print(f"回溯算法耗时: {time_backtrack:.6f}秒")
    print(f"迭代算法耗时: {time_iterative:.6f}秒")

if __name__ == "__main__":
    test_subsets()
